<?php
//前台默认首页控制器
class IndexControl extends Control{
    //前台首页显示
    function index(){
    	$where = 'isdel=0 && category.isoff=0';
    	$db = K('Article');
    	$total = $db->where($where)->count();
    	$page = new page($total, 8, 4, 2);
    	$this->assign('page',$page->show());

    	$field = 'title,time,cname,click,intro,aid';
    	$article = $db->field($field)->where($where)->order('istop DESC, time DESC')->select($page->limit());
    	$this->assign('article',$article);

    	$this->right();
        $this->display();
    }

    //右侧信息数据分配
    public function right(){
    	//栏目列表
    	$category = M('category')->where(array('isoff'=>0))->field('cname,cid')->select();
    	$this->assign('category',$category);
    	//最新文章
    	//最新文章
        $where = "isdel=0 && category.isoff=0";
        $articleDb = k('Article');
        $newArticle = $articleDb->where($where)->field('title,aid')->order('time DESC')->limit(6)->select();
        $this->assign('newArticle', $newArticle);
        //最新回复
        $commentDb = K('Comment');
        $newComment = $commentDb->where($where)->field('comment.content,comment.aid')->order('comment.time DESC')->limit(6)->select();
        $this->assign('newComment', $newComment);

        //站点统计
        $total = array();
        $total['article'] = $articleDb->where($where)->count();
        $total['comment'] = $commentDb->where($where)->count();
        $total['click'] = $articleDb->where($where)->sum('click');

        $this->assign('total', $total);

    }
    /**
     * 博文阅读
     */
    public function article(){
        $aid = $this->_GET("aid", 'intval');
        $field = 'title,time,cname,click,description,keywords,content';
        $where = array('aid'=>$aid);

        M('article')->inc('click', "aid=$aid", 1);
        $article = K('Article')->where($where)->field($field)->find();
        $this->assign('article', $article);

        $db = M('comment');
        $page = new page($db->where($where)->count(), 5, 4, 2);
        $this->assign('page', $page->show());

        $limit = $page->limit();
        $limitF = substr($limit['limit'], 0, 1);
        $this->assign('limitF', $limitF);

        $comment = $db->where($where)->select($limit);
        $this->assign('comment', $comment);

        $this->right();
        $this->display();
    }
    /**
     * 用户评论验证码显示
     */
    public function code(){
        $code = new code();
        $code->show();
    }
    /**
     * 用户发表评论
     */
    public function comment(){
        $code = $this->_POST('code', 'htmlspecialchars,strtoupper');
        if($code != session('code')) $this->error('验证码不正确');

        $data = array(
            'nickname'  => $this->_POST('nickname'),
            'content'   => $this->_POST('content'),
            'time'      => time(),
            'aid'       => $this->_POST('aid', 'intval')
            );
        M('comment')->add($data);
        $this->success('回复成功');
    }

    /**
     * 异步验证评论验证码
     * @return [type] [description]
     */
    public function ajax_code(){
        $code = $this->_POST('code', 'htmlspecialchars,strtoupper');
        if($code != session('code')) {
            echo 0;
        } else {
            echo 1;
        }
    }

    /**
     * 评论列表
     */
    public function article_list(){
        $where = "isdel=0 && category.isoff=0";
        $db = K('Article');
        $total = $db->where($where)->count();

        $page = new page ($total, 25, 4, 2);
        $this->assign('page', $page->show());

        $field = 'title,aid,time,istop';
        $article = $db->where($where)->field($field)->order('istop DESC,time DESC')->select($page->limit());
        $this->assign('article', $article);

        $this->right();
        $this->display();
    }

    /**
     * 栏目列表
     */
    public function category(){
        $cid = $this->_GET('cid', 'intval');
        $cname = M('category')->where(array('cid'=>$cid))->getField('cname');
        $this->assign('cname', $cname);

        $where = "isdel=0 && category.isoff=0";
        $db = K('Article');

        $total = $db->where($where)->count();
        $page = new page($total, 5, 4, 2);
        $this->assign('page', $page->show());

        $field = 'title,time,thumb,intro,aid';
        $article = $db->where($where)->field($field)->select($page->limit());
        $this->assign('article', $article);

        $this->right();
        $this->display();
    }


    //关于我
    public function about(){
        $this->display();
    }




}
?>